<?php

namespace common\models\es\dataProvider\comProducts;

use common\helpers\AmountHelper;
use common\models\es\EsComProducts;

class Query
{
    /**
     * 查询字段
     * @var array
     */
    protected $fields = [];

    /**
     * 查询条件
     * @var array
     */
    protected $query = [];

    /**
     * 排序方式
     * @var string
     */
    protected $sort = [];


    /**
     * 每页条数
     * @var
     */
    protected $size;


    /**
     * 偏移量
     * @var
     */
    protected $from;

    const SORT_CREATE_DESC = 1;//创建时间降序
    const SORT_CREATE_ASC = 2;//创建时间升序
    const SORT_PRICE_ASC = 3;//价格升序
    const SORT_PRICE_DESC = 4;//价格降序

    public function __construct(array $params, array $fields = [], int $page = 1, int $size = 20)
    {
        $page = $page ? $page : 1;
        $size = $size ? $size : 20;

        $offset = max(($page - 1), 0) * $size;

        $this->from = $offset;
        $this->size = $size;

        $this->formatSort($params);

        $this->formatFields($fields);

        $this->formatQuery($params);
    }

    /**
     * 查询字段
     * @param $fields
     */
    protected function formatFields($fields)
    {
        $es = new EsComProducts();
        $attributes = $es->attributes();
        $search_fields = [];
        foreach ($fields as $field) {
            if (in_array($field, $attributes)) {
                $search_fields[$field] = $field;
            }
        }
        $this->fields = $search_fields ? array_values($search_fields) : $attributes;
    }

    /**
     * 查询条件
     * @param $params
     */
    protected function formatQuery($params)
    {
        $query = [];
        if (isset($params['id']) && !empty($params['id'])) {
            $query['id'] = is_array($params['id']) ? $params['id'] : [$params['id']];
        }
        if (isset($params['ids']) && !empty($params['ids'])) {
            $query['ids'] = $params['ids'];
        }

        if (isset($params['num_iid']) && !empty($params['num_iid'])) {
            $query['num_iid'] = is_array($params['num_iid']) ? $params['num_iid'] : [$params['num_iid']];
        }

        if (isset($params['uid']) && !empty($params['uid'])) {
            $query['uid'] = intval($params['uid']);
        }



        if (isset($params['platform']) && !empty($params['platform'])) {
            $query['platform'] = intval($params['platform']);
        }

        if (isset($params['title']) && !empty($params['title'])) {
            $query['title'] = trim($params['title']);
        }

        if (isset($params['words']) && !empty($params['words'])) {
            $query['words'] = $params['words'];
        }

        if (isset($params['min_price']) && !empty($params['min_price'])) {
            $query['min_price'] = AmountHelper::yuanTofen(trim($params['min_price']));
        }

        if (isset($params['max_price']) && !empty($params['max_price'])) {
            $query['max_price'] = AmountHelper::yuanTofen(trim($params['max_price']));
        }

        if (isset($params['store_id']) && !empty($params['store_id'])) {
            $query['store_id'] = intval($params['store_id']);
        }

        if (isset($params['copy_status']) && !empty($params['copy_status'])) {
            $query['copy_status'] = intval($params['copy_status']);
        }

        if (isset($params['remark']) && !empty($params['remark'])) {
            $query['remark'] = trim($params['remark']);
        }

        if (isset($params['start_time']) && !empty($params['start_time'])) {
            $query['start_time'] = trim($params['start_time']);
        }

        if (isset($params['end_time']) && !empty($params['end_time'])) {
            $query['end_time'] = trim($params['end_time']);
        }

        $this->query = $query;
    }

    /**
     * 排序
     * @param $params
     */
    protected function formatSort($params)
    {
        $sort = $params['sort'] ?? static::SORT_CREATE_DESC;
        switch ($sort) {
            case static::SORT_PRICE_ASC://价格顺序
                $this->sort = ['prices' => ['order' => 'ASC']];
                break;
            case static::SORT_PRICE_DESC://价格倒序
                $this->sort = ['prices' => ['order' => 'DESC']];
                break;
            case static::SORT_CREATE_ASC:
                $this->sort = ['created_at' => ['order' => 'ASC']];
                break;
            default://默认最新
                $this->sort = ['created_at' => ['order' => 'DESC']];
                break;
        }
    }

    /**
     * 获取检索的索引名称
     * @return string
     */
    protected function getIndexName(): string
    {
        return EsComProducts::INDEX;
    }
}
